#!/usr/bin/ksh

service_file=nodemanager.xml

#------------------------------------------------------------------------------
# Return 1 if Running
#------------------------------------------------------------------------------
nmRunning(){
	sd_running=`svcs -a | grep 'svc:/application/management/nodemanager/weblogic:default'`
	if [[ $(echo $sd_running | grep -c 'online') == 0 ]]; then
		sd_running=`ps -ef` | grep 'Dnodemanager'
		if [[ $(echo $sd_running | grep -c 'online') == 0 ]]; then
			return 0
		fi			 	        
	else
	        return 1
	fi
}

#------------------------------------------------------------------------------
# Check if the user is root
#------------------------------------------------------------------------------
ID=`/usr/xpg4/bin/id -u`
if [[ $ID > 0 ]]; then
        echo "Must be root to install a Service Definition"
        exit 1
fi

#------------------------------------------------------------------------------
# Check that the service is not already installed and running
#------------------------------------------------------------------------------
nmRunning
if [[ $? > 0 ]]; then
	echo "Node Manager is already installed and running"
	exit 1
fi   

#------------------------------------------------------------------------------
# Validate the def.
#------------------------------------------------------------------------------
svccfg validate $service_file

if [[ $? > 0 ]]; then
        echo "Service definition failed validation, exiting.  Check the nodemanager.xml file."
        exit 1
else
        echo "Service definition file OK"
fi

#------------------------------------------------------------------------------
# import the definition
#------------------------------------------------------------------------------
svccfg import $service_file
if [[ $? > 0 ]];then
        echo "Failed to import Service definition.  Exiting"
        exit 1
fi

#------------------------------------------------------------------------------
# Verify the import
#------------------------------------------------------------------------------
sd_installed=`svcs -a | grep svc:/application/management/nodemanager/weblogic:default`
if [[ $sd_installed == "" ]]; then
        echo "Could not find the Service definition for Node Manager.  Exiting"
        exit 1
fi

#------------------------------------------------------------------------------
# Give execute to the start and stop script
#------------------------------------------------------------------------------
chmod +x ${ANT_NM_HOME}/S85nodemanager
chmod +x ${ANT_NM_HOME}/K17nodemanager
chmod +x ${ANT_NM_HOME}/*.sh


#------------------------------------------------------------------------------
# Start it
#------------------------------------------------------------------------------
sd_running=`svcadm enable svc:/application/management/nodemanager/weblogic:default`
if [[ $sd_running > 0 ]]; then
        echo "Error starting Node Manager.  Exiting"
        exit 1
fi

#------------------------------------------------------------------------------
# Test it 
#------------------------------------------------------------------------------
echo "Waiting 10 seconds to let the Node Manager come up"
sleep 10
nmRunning
if [[ $? > 0 ]]; then
	echo "SUCCESS. Node Manager is installed and running"
	exit 0
else 
	echo "Failed to install and start Node Manager.  This might be because the Node Manager is still starting. Run the following command to see if it comes online.  svcs -a | grep 'svc:/application/management/nodemanager/weblogic:default'" 
	exit 1
fi

